import { api } from '@/services/api';
import { ProForm, ProFormText } from '@ant-design/pro-components';
import { Card, message } from 'antd';
import React from 'react';

const PasswordReset: React.FC = () => {
  return (
    <Card title="修改密码">
      <ProForm
        onFinish={async (values) => {
          try {
            await api.accounts.accountsUsersChangePasswordCreate({
              changePasswordRequest: values
            });
            message.success('密码修改成功');
            return true;
          } catch (error) {
            message.error('密码修改失败');
            return false;
          }
        }}
      >
        <ProFormText.Password
          name="oldPassword"
          label="当前密码"
          rules={[
            {
              required: true,
              message: '请输入当前密码',
            },
          ]}
        />
        <ProFormText.Password
          name="newPassword"
          label="新密码"
          rules={[
            {
              required: true,
              message: '请输入新密码',
            },
            {
              min: 8,
              message: '密码长度不能小于8位',
            },
          ]}
        />
        <ProFormText.Password
          name="confirmPassword"
          label="确认新密码"
          rules={[
            {
              required: true,
              message: '请确认新密码',
            },
            ({ getFieldValue }) => ({
              validator(_, value) {
                if (!value || getFieldValue('newPassword') === value) {
                  return Promise.resolve();
                }
                return Promise.reject(new Error('两次输入的密码不一致'));
              },
            }),
          ]}
        />
      </ProForm>
    </Card>
  );
};

export default PasswordReset;
